<template>
  <div>
        <div id="postcomment">
			<h3>提交评论</h3>
			<p class="p"></p>
			<textarea placeholder="请输入您要评论的内容..." v-model="postcontent"></textarea>
			<mt-button type="primary" size="large" @click="postcomment">发表</mt-button>
		</div>
        <!--评论列表-->
        <div class="list">
            <h3>评论列表</h3>
        <div v-for="(item,index) in list">
             <p class="title">
                 <span>第{{index+1}}楼</span>
				<span>{{item.user_name}}</span>
                <span>{{item.add_time}}</span>
			</p>
			<ul class="mui-table-view">
				<li class="mui-table-view-cell mui-media">
					{{item.content}}
				</li>
			</ul>
        </div>
        </div>

        <mt-button type="danger" size="large" plain @click="getMore">加载更多</mt-button>
        
  </div>
</template>

<script>
import common from '../../kits/common.js';
import { Toast } from 'mint-ui';
export default {
  props:['id'],
  data(){
      return {
          postcontent:'',
          list:[],
          pageindex:0
      }
  },
  methods:{
      postcomment(){
          if(this.postcontent.trim().length<=0){
              Toast('请输入评论内容');
              return;
          }
        var url = common.apidomain + '/api/postcomment/' + this.id;
        this.$http.post(url,{content:this.postcontent},{emulateJSON:true}).then(function(res){
            Toast(res.body.message);
        })

        this.list = [{
                user_name:'匿名用户',
                add_time:new Date(),
                content:this.postcontent
            }].concat(this.list);
      },
      getpagelist(pageindex){
        pageindex = pageindex || 1;
        var url = common.apidomain + '/api/getcomments/' + this.id + '?pageindex=' + pageindex;
        this.$http.get(url).then(function(res){
            if(res.body.status != 0){
                Toast(rse.body.message);
                return;
            }
            this.list = this.list.concat(res.body.message);
        })
      },
      getMore(){
        this.pageindex++;
        this.getpagelist( this.pageindex);
      }
  },
  created(){
      this.getpagelist(1);
  }
}
</script>

<style scoped>
    #postcomment{
        padding:5px;
    }
    .list{
        padding:5px;
    }
    .title{
        padding: 5px;
        color: #6d6d72;
        font-size: 15px;
        background-color: rgba(0,0,0,0.1);
    }
</style>
